// HYG星表数据，包含更多亮星信息
// 字段说明: id, 赤经(度), 赤纬(度), 视星等, 光谱类型, 颜色索引
export const brightStars = [
  // 一等星及更亮的恒星
  { id: 24436, ra: 101.2871553, dec: -16.7161158, mag: -1.46, spectral: 'A1V', colorIdx: 1, name: '天狼星' },
  { id: 45238, ra: 130.2913482, dec: -52.7128301, mag: -0.74, spectral: 'F0Ib', colorIdx: 2, name: '老人星' },
  { id: 28403, ra: 109.6292911, dec: -60.8355309, mag: -0.27, spectral: 'G2V', colorIdx: 3, name: '南门二' },
  { id: 21421, ra: 217.0762108, dec: 19.1824896, mag: -0.05, spectral: 'K0III', colorIdx: 4, name: '大角星' },
  { id: 38277, ra: 279.2347348, dec: 38.7836889, mag: 0.03, spectral: 'A0V', colorIdx: 1, name: '织女星' },
  { id: 54061, ra: 165.4590220, dec: 45.9979913, mag: 0.08, spectral: 'G8III', colorIdx: 4, name: '五车二' },
  { id: 48908, ra: 87.9298079, dec: -8.2016370, mag: 0.12, spectral: 'B8Ia', colorIdx: 0, name: '参宿七' },
  { id: 29426, ra: 113.2833280, dec: 5.2418323, mag: 0.38, spectral: 'F5IV-V', colorIdx: 2, name: '南河三' },
  { id: 3597, ra: 310.4552461, dec: -57.2462074, mag: 0.46, spectral: 'B5V', colorIdx: 0, name: '水委一' },
  { id: 48913, ra: 88.7929385, dec: 7.4070638, mag: 0.5, spectral: 'M1-M2Ia-Iab', colorIdx: 5, name: '参宿四' },
  { id: 32349, ra: 271.7808450, dec: 38.7801416, mag: 0.77, spectral: 'A7IV-V', colorIdx: 1, name: '牛郎星' },
  { id: 29134, ra: 44.5086704, dec: 16.3916309, mag: 0.85, spectral: 'K5III', colorIdx: 5, name: '毕宿五' },
  { id: 45941, ra: 189.5940113, dec: -60.8307164, mag: 0.9, spectral: 'B1III', colorIdx: 0, name: '南十字座α' },
  { id: 50583, ra: 131.8723657, dec: -11.3366615, mag: 0.98, spectral: 'B1V', colorIdx: 0, name: '角宿一' },
  { id: 59461, ra: 247.3594079, dec: -26.4317500, mag: 1.09, spectral: 'M1.5Iab-Ib', colorIdx: 5, name: '心宿二' },
  { id: 21695, ra: 79.1723263, dec: 28.0261926, mag: 1.14, spectral: 'K0III', colorIdx: 4, name: '北河三' },
  { id: 37826, ra: 308.3544988, dec: 45.2803385, mag: 1.25, spectral: 'A2Ia', colorIdx: 1, name: '天津四' },
  { id: 52783, ra: 159.6055600, dec: 11.9672029, mag: 1.35, spectral: 'B7V', colorIdx: 0, name: '轩辕十四' },
  { id: 45827, ra: 188.7238682, dec: -59.2754021, mag: 1.25, spectral: 'B0.5IV', colorIdx: 0, name: '南十字座β' },
  { id: 4081, ra: 40.6699525, dec: -29.8654790, mag: 1.16, spectral: 'A3V', colorIdx: 1, name: '北落师门' },
  { id: 23408, ra: 95.2991638, dec: -28.7655983, mag: 1.5, spectral: 'B2II', colorIdx: 0, name: '大犬座ε' },
  { id: 48627, ra: 85.9575250, dec: 9.8636028, mag: 1.64, spectral: 'B0.5III', colorIdx: 0, name: '参宿五' },
  { id: 53910, ra: 163.7013909, dec: 46.3713611, mag: 1.79, spectral: 'A2IV', colorIdx: 1, name: '五车五' },
  { id: 38263, ra: 278.3346429, dec: 45.4508750, mag: 1.93, spectral: 'A8IV', colorIdx: 1, name: '天津二' },
  { id: 24971, ra: 103.9260180, dec: -25.1970556, mag: 1.5, spectral: 'B2IV', colorIdx: 0, name: '弧矢七' },
  
  // 二等星（增加更多星星使星空更真实）
  { id: 32715, ra: 274.2048333, dec: 45.5980556, mag: 2.01, spectral: 'K3III', colorIdx: 5, name: '天津一' },
  { id: 59037, ra: 245.8920833, dec: 6.3572222, mag: 2.05, spectral: 'K1II-III', colorIdx: 4, name: '尾宿五' },
  { id: 33832, ra: 279.1945833, dec: 15.1644444, mag: 2.06, spectral: 'B3V', colorIdx: 0, name: '天蝎座α' },
  { id: 50743, ra: 133.3812500, dec: 4.0563889, mag: 2.07, spectral: 'B8V', colorIdx: 0, name: '室女座α' },
  { id: 26727, ra: 108.6158333, dec: 19.8991667, mag: 2.07, spectral: 'B9V', colorIdx: 0, name: '双子座β' },
  { id: 27225, ra: 109.7970833, dec: 28.0530556, mag: 2.14, spectral: 'A0V', colorIdx: 1, name: '双子座α' },
  { id: 48783, ra: 86.8412500, dec: -1.2844444, mag: 2.21, spectral: 'B1III', colorIdx: 0, name: '参宿六' },
  { id: 33579, ra: 277.1608333, dec: -1.9447222, mag: 2.23, spectral: 'B5V', colorIdx: 0, name: '天蝎座β' },
  { id: 31564, ra: 265.1425000, dec: -30.6347222, mag: 2.25, spectral: 'A5IV', colorIdx: 1, name: '南鱼座α' },
  { id: 47783, ra: 84.1858333, dec: 11.9333333, mag: 2.26, spectral: 'B3V', colorIdx: 0, name: '猎户座γ' },
  
  // 猎户座主要恒星
  { id: 49933, ra: 89.3291667, dec: -0.3847222, mag: 2.23, spectral: 'B0.5V', colorIdx: 0, name: '猎户座δ' },
  { id: 50281, ra: 90.2958333, dec: 0.4069444, mag: 1.77, spectral: 'B0V', colorIdx: 0, name: '猎户座ε' },
  { id: 50480, ra: 90.7408333, dec: 1.1380556, mag: 2.21, spectral: 'B0V', colorIdx: 0, name: '猎户座ζ' },
  { id: 50516, ra: 90.8508333, dec: 1.8486111, mag: 2.42, spectral: 'B2V', colorIdx: 0, name: '猎户座η' },
  { id: 49318, ra: 87.9850000, dec: 0.1813889, mag: 3.39, spectral: 'O9.5V', colorIdx: 0, name: '猎户座θ1' },
  { id: 49321, ra: 87.9875000, dec: 0.1947222, mag: 4.29, spectral: 'B1V', colorIdx: 0, name: '猎户座θ2' }
];

// 星座连线数据（使用国际天文联合会认可的星座边界和连线）
export const constellationLines = {
  // 猎户座 - 更完整的猎户座连线
  orion: [
    [48913, 48627], // 参宿四 - 参宿五
    [48627, 47783], // 参宿五 - 参宿γ
    [47783, 49933], // 参宿γ - 参宿δ
    [49933, 50281], // 参宿δ - 参宿ε
    [50281, 50480], // 参宿ε - 参宿ζ
    [50480, 50516], // 参宿ζ - 参宿η
    [50516, 48908], // 参宿η - 参宿七
    [48908, 23408], // 参宿七 - 大犬座ε
    [23408, 48913], // 大犬座ε - 参宿四
    // 猎户座腰带上的三颗星
    [49318, 49321], // 猎户座θ1 - θ2
    [49318, 49933], // 连接到参宿δ
    [50281, 49321]  // 连接到参宿ε
  ],
  // 天鹰座 - 完整的天鹰座连线
  aquila: [
    [32349, 37826], // 牛郎星(天鹰座α) - 天津四(天鹰座α)
    [37826, 38263], // 天津四 - 天津二
    [38263, 32715], // 天津二 - 天津一
    [32715, 32349], // 天津一 - 牛郎星
    [32349, 38263]  // 对角线
  ],
  // 天琴座 - 天琴座四边形
  lyra: [
    [38277, 38263], // 织女星(天琴座α) - 天津二
    [38263, 38277], // 闭环
    [38277, 38277]  // 简化表示
  ],
  // 大犬座
  canisMajor: [
    [24436, 23408], // 天狼星(大犬座α) - 大犬座ε
    [23408, 24971], // 大犬座ε - 弧矢七
    [24971, 24436], // 弧矢七 - 天狼星
    [24436, 24971]  // 直接连线
  ],
  // 双子座
  gemini: [
    [27225, 26727], // 北河二(双子座α) - 北河三(双子座β)
    [27225, 27225], // 简化表示
    [26727, 26727]  // 简化表示
  ],
  // 天蝎座
  scorpius: [
    [59461, 33579], // 心宿二(天蝎座α) - 天蝎座β
    [33579, 33832], // 天蝎座β - 天蝎座δ
    [33832, 59461]  // 简化闭环
  ]
};

// 星座名称和中心点（使用更准确的位置计算）
export const constellations = [
  { 
    name: '猎户座', 
    ra: 85, 
    dec: 0, 
    position: { x: 0, y: 0, z: -50 },
    abbreviation: 'Ori',
    boundary: '冬季星座'
  },
  { 
    name: '天鹰座', 
    ra: 300, 
    dec: 25, 
    position: { x: 50, y: 30, z: -20 },
    abbreviation: 'Aql',
    boundary: '夏季星座'
  },
  { 
    name: '天琴座', 
    ra: 279, 
    dec: 40, 
    position: { x: -30, y: 40, z: -20 },
    abbreviation: 'Lyr',
    boundary: '夏季星座'
  },
  { 
    name: '大犬座', 
    ra: 100, 
    dec: -25, 
    position: { x: 40, y: -30, z: -30 },
    abbreviation: 'CMa',
    boundary: '冬季星座'
  },
  { 
    name: '双子座', 
    ra: 70, 
    dec: 25, 
    position: { x: -40, y: 25, z: -30 },
    abbreviation: 'Gem',
    boundary: '冬季星座'
  },
  { 
    name: '天蝎座', 
    ra: 245, 
    dec: -20, 
    position: { x: -20, y: -20, z: -40 },
    abbreviation: 'Sco',
    boundary: '夏季星座'
  },
  { 
    name: '室女座', 
    ra: 130, 
    dec: 0, 
    position: { x: 20, y: 0, z: -50 },
    abbreviation: 'Vir',
    boundary: '春季星座'
  },
  { 
    name: '南鱼座', 
    ra: 330, 
    dec: -30, 
    position: { x: 30, y: -30, z: -40 },
    abbreviation: 'PsA',
    boundary: '秋季星座'
  }
];

// 光谱类型对应的颜色
export const spectralColors = {
  'O': 0x9bb0ff,  // 蓝紫色
  'B': 0xaabbff,  // 蓝色
  'A': 0xd5eaff,  // 淡蓝色
  'F': 0xfff4dd,  // 淡橙色
  'G': 0xffd1a1,  // 橙色
  'K': 0xffb366,  // 橙红色
  'M': 0xff9966   // 红色
};

// 简化的颜色索引映射
export const colorMap = [
  0xaabbff,  // 0: 蓝色 (B型星)
  0xd5eaff,  // 1: 淡蓝色 (A型星)
  0xfff4dd,  // 2: 淡橙色 (F型星)
  0xffd1a1,  // 3: 橙色 (G型星)
  0xffb366,  // 4: 橙红色 (K型星)
  0xff9966   // 5: 红色 (M型星)
];

// 根据视星等计算星点大小 - 优化的计算方法更符合人眼视觉特性
export function calculateStarSize(magnitude, baseSize = 1.0) {
  // 视星等越小，星点越大
  // 优化的对数关系模型，更准确地模拟人眼对亮度的非线性感知
  // 使用base-2对数，符合星等的对数定义（每相差5个星等，亮度相差100倍）
  const size = baseSize * Math.pow(2, (-magnitude + 6) / 2.5);
  
  // 添加最小和最大大小限制，确保渲染效果良好
  return Math.max(Math.min(size, 10.0), 0.1);
}

// 计算恒星的闪烁效果
export function calculateTwinkleFactor(baseFactor = 0.05) {
  // 生成随机的闪烁因子
  return 1.0 + (Math.random() - 0.5) * baseFactor;
}

// 从光谱类型提取主类型
export function getSpectralType(spectralClass) {
  if (!spectralClass) return 'Unknown';
  return spectralClass.charAt(0).toUpperCase();
}

// 根据星等过滤星星
export function filterStarsByMagnitude(stars, maxMagnitude = 2.5) {
  return stars.filter(star => star.mag <= maxMagnitude);
}

// 赤道坐标转笛卡尔坐标 - 优化的坐标转换算法
export function equatorialToCartesian(ra, dec, distance = 100) {
  // 将角度转换为弧度
  const raRad = (ra / 180) * Math.PI;
  const decRad = (dec / 180) * Math.PI;
  
  // 标准球坐标到笛卡尔坐标的转换
  // 注意：这里使用天文学标准坐标系，赤经从春分点向东测量
  const cosDec = Math.cos(decRad);
  const x = distance * cosDec * Math.cos(raRad);
  const y = distance * Math.sin(decRad);
  const z = distance * cosDec * Math.sin(raRad);
  
  return { x, y, z };
}

// 根据星座名称获取其中心点坐标（优化的坐标计算）
export function getConstellationCenter(constellationName, scale = 100) {
  const constellation = constellations.find(c => c.name === constellationName);
  if (!constellation) return { x: 0, y: 0, z: 0 };
  
  // 使用赤道坐标计算更准确的中心点
  return equatorialToCartesian(constellation.ra, constellation.dec, scale * 0.9);
}